#include "Effects.h"
#include "../utils/Logger.h"

Effects::Effects() {
}

Effects::~Effects() {
    while (any())
        delete pop();
}

void Effects::clear() {
    effects.clear();
}

void Effects::push(Effect *effect) {
    effects.push_back(effect);
}

Effect* Effects::pop() {
    if (effects.empty())
        return NULL;
    else {
        Effect *effect = top();
        effects.pop_back();
        return effect;
    }
}

Effect* Effects::top() {
    if (effects.empty())
        return NULL;
    else
        return effects.back();
}

const Effect* Effects::top() const {
    if (effects.empty())
        return NULL;
    else
        return effects.back();
}

bool Effects::empty() const {
    return effects.empty();
}

bool Effects::any() const {
    return !effects.empty();
}

long unsigned int Effects::size() const {
    return effects.size();
}

IplImage* Effects::apply(const IplImage* in) {
    Log << "Applying list of effect: " << effects.size() << endl;

    IplImage *src, *dst;

    src = cvCreateImage(cvSize(in->width, in->height), in->depth, in->nChannels);
    cvCopy(in, src);
    dst = src;

    for (deque<Effect*>::iterator it = effects.begin();
            it != effects.end(); it++)
    {
        Effect* effect = *it;
        dst = effect->apply(src);
        cvReleaseImage(&src);
        src = dst;
    }

    return dst;
}
